<?php
$a = $datay = array();

$year_arr = array();

$sql = "SELECT distinct date_part('year'::text,the_date) as year from statistics order by year asc";
$res = pg_query($sql);
$num = pg_num_rows($res);
while ($dat = pg_fetch_array($res)) {
	$tahun = $dat['year'];
	$a[] = $dat['year'];

	
	$sql_2 = "select item_id,agency_id,the_date,the_value,date_part('year'::text,the_date) as year from statistics where agency_id='1' and  date_part('year'::text,the_date)='$tahun' and item_id=$item_id";
	$res_2 = pg_query($sql_2);
	$sum = 0;
	while ($dat_2 = pg_fetch_array($res_2)) {
		$sum = $sum + $dat_2['the_value'];
	}
	$datay[] = $sum;
	
}
//print_r($datay);exit;


//print_r($datay);exit;
// Create the graph. These two calls are always required
$graph = new Graph(550,300,"auto");	
$graph->SetScale("textlin");
$graph->yaxis->scale->SetGrace(20);
$graph->xaxis-> SetTickLabels($a);

// Add a drop shadow
//$graph->SetShadow();

// Adjust the margin a bit to make more room for titles
$graph->img->SetMargin(40,30,20,40);

// Create a bar pot
$bplot = new BarPlot($datay);

// Adjust fill color
$bplot->SetFillColor('orange');
$bplot->SetShadow('#aaaaaa');
$bplot->value->Show();
$bplot->value->SetFont(FF_ARIAL,FS_NORMAL,9);
$bplot->value->SetAngle(0);
$bplot->value->SetFormat('%0.0f');
$graph->Add($bplot);

// Setup the titles
$graph->title->Set("Energy Usage Yearly Statistic");
$graph->title->Set("");
$graph->xaxis->title->Set("Year");
$graph->yaxis->title->Set("1000Watt");

$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

$graph->SetBackgroundGradient('blue:1.9','blue:1.9',GRAD_HOR,BGRAD_MARGIN); 

// Display the graph
$graph->Stroke();
?>
